package zhou.teamwork14;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CandidateDao {
		BaseDao bd=new BaseDao();
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		//鏁版嵁搴撳叧闂�
		public void close() throws SQLException{
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
		}
		//鏁版嵁搴撳�欓�変汉淇℃伅鏌ヨ锛�
		public Candidate selectCandidateByName(String name) throws SQLException{
			con=bd.getCon();
			String sql="select *from candidate where name=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, name);
			rs=ps.executeQuery();
			int vote=0;
			String group=null;
			while(rs.next()){
				vote=rs.getInt("vote");
				group=rs.getString("group");
			}
			Candidate c=new Candidate(group,name,vote);
			return c;
		}
		public boolean insertVote(String name) throws SQLException{
			boolean result=false;
			Candidate cd1=new Candidate();
			cd1=selectCandidateByName(name);
			int vote=cd1.getVote();
			vote++;
			con=bd.getCon();
			String sql="insert into candidate (vote) values(vote=?)";
			ps=con.prepareStatement(sql);
			ps.setInt(1, vote);
			int a=ps.executeUpdate();
			if(a>0){
				result=true;
			}
			return result;
		}
}
